package com.example.qfsx;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private final List<PopularGood> productList;

    public ProductAdapter(List<PopularGood> productList) {
        this.productList = productList;
    }

    public static class ProductViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView titleTextView;
        public TextView priceTextView;

        public ProductViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_product);
            titleTextView = itemView.findViewById(R.id.text_title);
            priceTextView = itemView.findViewById(R.id.text_price);
        }
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        PopularGood product = productList.get(position);
        holder.titleTextView.setText(product.getGoodsName());
        holder.priceTextView.setText(String.valueOf(product.getShopPrice()));

        // 使用Glide加载图片
        Glide.with(holder.itemView.getContext())
                .load(product.getGoodsImg())
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }
}